package com.gzhuxj201.controller;

import com.gzhuxj201.entity.Class;
import com.gzhuxj201.entity.Result;
import com.gzhuxj201.service.ClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    @GetMapping
    public Result lists(){
        List<Class> classes = classService.selectAll();
        return Result.success(classes);
    }

    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable int id){
        int re = classService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Class cla){
        int re= classService.add(cla);
        if(re>0){
            return Result.success();
        }else {
            return Result.error("添加失败");
        }
    }

    @GetMapping("{id}")
    public Result selectById(@PathVariable int id){
        Class dept = classService.selectById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Class cla){
        int re=classService.update(cla);
        if(re>0){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }

    }
}
